<?php

namespace app\index\controller;

use think\Db;

class Message extends Home
{
    protected $beforeActionList = [
        'check_load'
    ];
    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 用户留言
     */
    public function liuyan()
    {
        $uid = is_login();
        if (request()->isPost()) {
            $data = input('post.');
            if(!$data['content']){
                $this->error('请输入信息内容');
            }
            $data['fromuserid'] = $uid;
            $data['touser'] = 0;
            $data['create_time'] = time();
            $data['status'] = 1;
            $res = Db('liuyan')->insert($data);
            if ($res) {
                $this->success('留言成功', url('liuyanList', array('type' => 2)));
            } else {
                $this->error('留言失败');
            }
        } else {
            $this->assign('title', '写信息');
            return $this->fetch();
        }
    }

    /**
     * 留言列表
     */
    public function liuyanList()
    {
        $uid = is_login();
        $maps['begintime'] = $starttime = request()->post('begintime', date("Y-m-d", strtotime('-12 month')));
        $maps['endtime'] = $endtime = request()->post('endtime', date("Y-m-d", time()));
        $maps['type'] = $type = input('type', 1);

        $starttime = strtotime("$starttime 00:00:00");
        $endtime = strtotime("$endtime 23:59:59");
        $map['create_time'] = [['egt', $starttime], ['elt', $endtime]];

        $map['status'] = array('egt', 1);
        if ($type == 1) {
            $list = Db('liuyan')->where($map)->where("touser = {$uid} and status = 1")->whereOr("fromuserid = {$uid} and status = 2")->order('create_time desc')->select();
            $this->assign('title', '收件箱');
        }

        if ($type == 2) {
            $list = Db('liuyan')->where($map)->where("touser = {$uid} and status = 2")->whereOr("fromuserid = {$uid} and status = 1")->order('create_time desc')->select();
            $this->assign('title', '发件箱');
        }

        $this->assign('_list', $list);
        /*查询条件*/
        $opt = array(
            array(
                'type' => 'input',
                'ipttype' => 'hidden',
                'name' => 'type',
                'value' => $maps['type'],
            )
        );
        $this->searchCondition($maps);

        $this->assign('uid', $uid);
        $this->assign('type', $type);
        return $this->fetch('liuyanlist');
    }

    /**
     * 留言回复
     */
    public function liuyanReply()
    {
        $id = input('id');
        $liuyan = Db('Liuyan')->where('id' , $id)->find();
        if (request()->isPost()) {
            $data = input('post.');
            if(!$data['reply']){
                $this->error('请输入回复内容');
            }
            $data['reply_time'] = time();
            $data['id'] = $id;
            $data['status'] = 2;
            $res = Db('Liuyan')->update($data);
            if ($res) {
                $this->success('回复成功', url('liuyanList', array('type' => 1)));
            } else {
                $this->error('回复失败');
            }
        } else {
            $this->assign('liuInfo', $liuyan);
            $this->assign('title', '留言回复');
            return $this->fetch('liuyanreply');
        }
    }

    /**
     * 删除留言信息
     */
    public function deleteLiuyan()
    {
        $id = I('id');
        $map['id'] = $id;
        $type = I('type');
        $status = I('status');
        if ($type == 1) {
            $res = M('Liuyan')->where($map)->setField('tostatus', 0);
        } else if ($type == 2) {
            if ($status == 0) {
                $res = M('Liuyan')->where($map)->delete();
            } else {
                $res = M('Liuyan')->where($map)->setField('fromstatus', 0);
            }
        }
        $this->success('删除成功', U('liuyanList', array('type' => $type)));
    }
}

